<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>订单管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container" id="app">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="" lay-filter="table-search">
                    <div class="layui-inline">
                        <label class="layui-form-label">选择区服</label>
                        <div class="layui-input-inline" style="width: 200px">
                            <div id="gameArea"></div>
                        </div>
                    </div>

                    <div class="layui-inline">
                        <label class="layui-form-label">选择渠道</label>
                        <div class="layui-input-inline" style="width: 200px">
                            <div id="channel"></div>
                        </div>
                    </div>
                    <div class="layui-inline" >
                        <label class="layui-form-label">记录时间</label>
                        <div class="layui-input-inline">
                            <input type="text" name="createDateBegin" id="createTime0"
                                   placeholder="开始时间" autocomplete="off" class="layui-input" />
                            <input type="text" name="createDateEnd" id="createTime1"
                                   placeholder="截止时间" autocomplete="off" class="layui-input" />
                        </div>
                    </div>

                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn1"><i class="layui-icon"></i> 合计搜索</button>
                        </div>

                </form>

            </div>

        </fieldset>
    </div>
</div>

<table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-normal layui-btn-sm" auth="payOrder:export" lay-event="export">导出</button>
    </div>
</script>
<script type="text/html" id="currentTableBar">
</script>
<script src="/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="/js/common.js?v=5" charset="utf-8"></script>
<script src="/xm-select-v1.2.4/xm-select.js"></script>

<script>
    var form,$,_this;
    var services = JSON.parse(localStorage.getItem('services'))
    var channeldata = {
        option1: {
            name: "修魔抖音",
            channel: 'xmdy'
        },
        option2: {
            name: "辰画",
            channel: 'chenhua'
        },
        option3: {
            name: "趣智连",
            channel: 'qzl'
        }
    };
    new Vue({
        el: "#app",
        data: {
            services:services,
            paySum:0,
        },
        methods:{
            seePaySum:function () {
                var formData = form.val('pay-order-sta')
                $.get('/admin/payOrder/sta',formData,function (res) {
                    if (res.code!==0) {
                        layer.msg(res.msg,{icon:2})
                        return;
                    }
                    _this.paySum = res.data.paySum +"元"
                })
            }
        },
        created:function(){
            _this = this;
        },
        mounted:function () {
            layui.use(['form', 'table','laydate'], function () {
                form = layui.form;
                $ = layui.jquery;
                var isLodedTable = false;

                var table = layui.table;
                var laydate=layui.laydate;

                var data = [];
                for(var k in services){
                    var v = services[k];
                    data.push({
                        name:v.name,
                        value:v.server_id,
                    })
                }

                laydate.render({
                    elem: '#createTime0',
                    type:'datetime',
                    value: getYmd(0)+ " 00:00:00"
                });
                laydate.render({
                    elem: '#createTime1',
                    type:'datetime',
                    value: getYmd(1) + " 00:00:00"
                });
                xmSelect.render({
                    el: '#gameArea',
                    name:"gameArea",
                    language: 'zn',
                    data: data
                });


                var cdata=[]
                for (var k in channeldata) {
                    var v = channeldata[k];
                    cdata.push({
                        name: v.name,
                        value: v.channel
                    });
                }
                xmSelect.render({
                    el: '#channel',
                    name:"channel",
                    language: 'zn',
                    data: cdata
                });

                var cols = [
                    [
                        { title: '日期', field: "date" },
                        { title: '渠道', field: "category" },
                        { title: '订单额', field: "fee" },
                        { title: '真实付款', field: "front_fee" },
                        { title: '差异值', field: "Dvalue" },
                        { title: '合计真实付款', field: "total_front_fee" },
                    ]
                ];

                var  newcols = [
                    [
                        { title: '日期', field: "date" },
                        { title: '真实付款', field: "total_fee" },
                        { title: '订单金额', field: "total_front_fee" },
                        { title: '差异金额', field: "total_Dvalue" },
                    ]
                ];
                var originalData; // 原始数据

                var loadTable = function() {


                    table.render({
                        where: form.val('table-search'),
                        elem: '#currentTableId',
                        url: '/admin/payOrder/report',
                        defaultToolbar: ['filter', 'print'],
                        width: 2300,
                        toolbar: '#toolbarDemo',
                        cols: cols,
                        limits: [10, 15, 20, 25, 50, 100],
                        limit: 15,
                        page: true,
                        skin: 'line',
                        data:originalData
                    });

                    isLodedTable = true;
                };

                // 初始加载列表
                loadTable();// 监听搜索操作
                form.on('submit(data-search-btn1)', function(data) {
                    // 更新列表的列定义
                    //执行搜索重载
                    table.reload('currentTableId', {
                        page: {
                            curr: 1
                        },
                        cols:newcols
                        , where: form.val('table-search'),
                        parseData: function(res) {
                        // 处理数据，筛选出不重复的日期
                        var uniqueDates = [];
                        res.data.forEach(function(item) {
                            if (!uniqueDates.includes(item.date)) {
                                uniqueDates.push(item.date);
                            }
                        });
                            // 构造新的数据
                            var newData = uniqueDates.map(function(date) {
                                return {
                                    date: date,
                                    total_fee: res.data.find(function(item) {
                                        return item.date === date;
                                    }).total_fee,
                                    total_front_fee: res.data.find(function(item) {
                                        return item.date === date;
                                    }).total_front_fee,
                                    total_Dvalue: res.data.find(function(item) {
                                        return item.date === date;
                                    }).total_Dvalue
                                };
                            });
                            // 修改返回的数据
                            res.data = newData;
                        console.log(res)
                        return res;

                    }
                    }, 'data');

                    return false;
                });
                form.render()
                // 监听搜索操作
                form.on('submit(data-search-btn)', function (data) {
                    if (!isLodedTable){
                        loadTable();
                    } else {
                        //执行搜索重载
                        table.reload('currentTableId', {
                            page: {
                                curr: 1
                            },
                            cols:cols
                            , where: form.val('table-search')

                        }, 'data');
                        loadTable();
                    }

                    return false;
                });

                // 监听按钮点击事件
                table.on('toolbar(currentTableFilter)', function (obj) {
                    if (obj.event === 'export') {
                        var searchData = form.val('table-search');
                        searchData.page = 1;
                        searchData.limit = 9999999;
                        var layerIndex = layer.msg("请求中请耐心等待...",{icon:16,time:999999999});
                        //layer.close(layerIndex)
                        $.get('/admin/payOrder/export',searchData,function(res){
                            layer.close(layerIndex)
                            browserDownload( "/" + res.data);
                        });

                    }
                });
                form.render()
            });
        }
    })

</script>
</body>
</html>